package com.zhuiyi.yiqingbackend.controller;

import com.zhuiyi.yiqingbackend.common.BaseResponse;
import com.zhuiyi.yiqingbackend.common.ErrorCode;
import com.zhuiyi.yiqingbackend.common.ResultUtils;
import com.zhuiyi.yiqingbackend.domain.Cure;
import com.zhuiyi.yiqingbackend.domain.request.AddCureRequest;
import com.zhuiyi.yiqingbackend.domain.request.AddCureRequest1;
import com.zhuiyi.yiqingbackend.domain.request.OnlyBaseId;
import com.zhuiyi.yiqingbackend.exception.BusinessException;
import com.zhuiyi.yiqingbackend.service.CureService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@Api(value = "接口", tags = "治愈人员管理接口")
@RequestMapping("/cure")
@CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
public class CureController {
    @Resource
    private CureService cureService;

    @GetMapping("/getCures")
    @ApiOperation(value = "获取治愈人员列表", notes = "获取治愈人员列表")
    public BaseResponse<List<Cure>> getCures(){
        List<Cure> cures = cureService.getCures();
        return ResultUtils.success(cures);
    }

    @GetMapping("/getCureById/{id}")
    @ApiOperation(value = "根据id获取治愈人员", notes = "根据id获取治愈人员")
    public BaseResponse<Cure> getCureById(@PathVariable int id){
        Cure cure = cureService.getCureById(id);
        return ResultUtils.success(cure);
    }

    @PostMapping("/addCure")
    @ApiOperation(value = "添加治愈人员", notes = "添加治愈人员")
    public BaseResponse<Boolean> addCure(@RequestBody AddCureRequest request){
        boolean ok = cureService.addCure(request);
        if(!ok){
            throw new BusinessException(ErrorCode.FAIL, "添加失败");
        }
        return ResultUtils.success(ok);
    }
    @PostMapping("/addCure1")
    @ApiOperation(value = "添加治愈人员 plus ", notes = "添加治愈人员 plus ")
    public BaseResponse<Boolean> addCure1(@RequestBody AddCureRequest1 request){
        boolean ok = cureService.addCure1(request);
        return ResultUtils.success(ok);
    }
    @PostMapping("/addCure2")
    @ApiOperation(value = "添加治愈人员 plus ", notes = "添加治愈人员 plus ")
    public BaseResponse<Boolean> addCure2(@RequestBody OnlyBaseId onlyBaseId){//虽然只有1个参数，但用RequestBody 接受就得用以请求体来接受
        int id = onlyBaseId.getId();
//        System.out.println(id);
        boolean ok = cureService.addCure2(id);
        return ResultUtils.success(ok);
    }

    @GetMapping("/deleteCure/{id}")
    @ApiOperation(value = "删除治愈人员", notes = "删除治愈人员")
    public BaseResponse<Boolean> deleteCure(@PathVariable int id){
        boolean ok = cureService.deleteCure(id);
        if(!ok){
            throw new BusinessException(ErrorCode.FAIL, "删除失败");
        }
        return ResultUtils.success(ok);
    }
}
